<template>
  <div class="app-container">
    <el-card class="box-card operate-container">
      <div slot="header" class="clearfix">
        <span>文件筛选</span>
      </div>
      <div class="">
        <!-- 打开文件 -->
        <div class="openFile">
          <el-row :gutter="20">
            <el-col :span="6">
              <el-upload
                class="upload-demo"
                ref="upload"
                action="https://"
                :on-remove="handleRemove"
                :file-list="fileList"
                :auto-upload="false"
              >
                <el-button slot="trigger" size="medium" type="primary"
                  >选取文件</el-button
                >
                <!-- <el-button
                  style="margin-left: 10px"
                  size="medium"
                  type="success"
                  @click="submitUpload"
                  >上传到服务器</el-button
                > -->
                <div slot="tip" class="el-upload__tip">
                  上传bat文件，且不超过10M
                </div>
              </el-upload>
            </el-col>
            <el-col :span="18">
              <el-input
                v-model="file.name"
                :disabled="true"
                placeholder="文件名称"
              ></el-input>
            </el-col>
          </el-row>
        </div>
        <!-- 查询 -->
        <div class="query">
          <el-form ref="form" :model="listQuery" label-width="120px">
            <el-row :gutter="20">
              <el-col :span="6">
                <el-form-item label="母线名前缀">
                  <el-select
                    v-model="listQuery.busPrefix"
                    placeholder="请选择母线名前缀"
                    clearable
                  >
                    <el-option
                      v-for="item in busPrefixOptions"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value"
                    >
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="电压等级">
                  <el-select
                    clearable
                    v-model="listQuery.voltageLevel"
                    placeholder="请选择电压等级"
                  >
                    <el-option
                      v-for="item in voltageLevelOptions"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value"
                    >
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>

              <el-col :span="12">
                <el-form-item>
                  <el-button type="success">重置</el-button>
                  <el-button type="primary" @click="onSubmit"
                    >模糊查询</el-button
                  >
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </div>
      </div>
    </el-card>
    <div class="table-container">
      <el-table
        v-loading="listLoading"
        :data="list"
        border
        element-loading-text="加载中..."
        fit
        highlight-current-row
        height="600"
        style="width: 100%"
      >
        <el-table-column
          align="center"
          label="序号"
          width="60"
          type="index"
          fixed="left"
        >
        </el-table-column>
        <el-table-column align="center" label="所属地市ID" prop="subName">
        </el-table-column>
        <el-table-column align="center" label="变电站名称" prop="subName">
        </el-table-column>
        <el-table-column align="center" label="变电站名称(映射)" prop="subName">
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      list: null,
      listLoading: false,
      file: {
        name: "",
      },
      listQuery: {
        busPrefix: "", //母线名前缀
        voltageLevel: "", //电压等级
      },
      busPrefixOptions: [
        {
          value: "0",
          label: "沪",
        },
        {
          value: "1",
          label: "苏",
        },
        {
          value: "2",
          label: "京",
        },
        {
          value: "3",
          label: "鲁",
        },
      ],
      voltageLevelOptions: [
        {
          value: "0",
          label: "220KV",
        },
        {
          value: "1",
          label: "110KV",
        },
        {
          value: "2",
          label: "50KV",
        },
        {
          value: "3",
          label: "20KV",
        },
      ],
      fileList: [],
      fileContent: "", // .bat文件内容
      data: [], // 数据行信息
    };
  },
  methods: {
    onSubmit() {
      console.log("submit!");
    },
    handleRemove() {
      console.log("000");
    },
    handleUpload(event) {
      const file = event.target.files[0];

      if (file === undefined || !file.type.includes("text/plain")) {
        alert("只能上传txt类型的文件");
        return;
      }

      this.readBatFile(file);
    },

    readBatFile(file) {
      const reader = new FileReader();

      reader.onload = () => {
        this.parseBatFile(reader.result);
      };

      reader.readAsText(file);
    },

    parseBatFile(content) {
      // 根据具体情况编写正则表达式或其他处理方法来提取数据
      // 此处仅作示例，假设每行数据由空格分隔
      const lines = content.split("\n");
      let headersFound = false;

      for (let i = 0; i < lines.length; i++) {
        const line = lines[i].trim();

        if (!headersFound) {
          // 第一行为列头信息
          this.headers = line.split(" ").filter((value) => value !== "");
          headersFound = true;
        } else {
          // 从第二行开始为数据行信息
          this.data.push(line.split(" "));
        }
      }

      this.dataLoaded = true;
    },
  },
};
</script>

<style lang="scss" scoped>
.mainBox {
  width: 100%;
  height: 100%;
  background-color: aqua;

  .box-card {
    width: 96%;
  }
}
.query {
  margin: 50px 0;
}
</style>
